﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HeaderViewModel.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Header view model
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.Silverlight.MVVM.ViewModels
{
    #region Using Directives

    using System;
    using System.Reflection;
    using System.Windows.Browser;
    using System.Windows.Input;
    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;
    using Messages;

    using Spenta.StreetCare.Silverlight.Globals;
    using Spenta.StreetCare.Silverlight.Logging;

    using StreetCareAPI;

    #endregion

    /// <summary>
    /// Header view model
    /// </summary>
    public class HeaderViewModel : BaseViewModel
    {
        /// <summary>
        /// Private store for the user
        /// </summary>
        private User user;

        /// <summary>
        /// Private store for the customer
        /// </summary>
        private Customer customer;

        #region Class Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="HeaderViewModel"/> class.
        /// </summary>
        public HeaderViewModel()
        {
            if (HtmlPage.IsEnabled)
            {
                Messenger.Default.Register<Customer>(this, c => Customer = c);
                ApiClient.GetUserIdentityCompleted += this.GetUserIdentityCompleted;
                ApiClient.GetUserIdentityAsync();
            }
        }

        #endregion

        /// <summary>
        /// Gets a value indicating whether this <see cref="HeaderViewModel"/> is anonymous.
        /// </summary>
        /// <value>
        ///   <c>true</c> if anonymous; otherwise, <c>false</c>.
        /// </value>
        public bool Anonymous
        {
            get { return User == null; }
        }

        /// <summary>
        /// Gets a value indicating whether this <see cref="HeaderViewModel"/> is authenticated.
        /// </summary>
        /// <value>
        ///   <c>true</c> if authenticated; otherwise, <c>false</c>.
        /// </value>
        public bool Authenticated
        {
            get { return User != null; }
        }

        /// <summary>
        /// Gets or sets the connected user.
        /// </summary>
        /// <value>
        /// The connected user.
        /// </value>
        public User User
        {
            get
            {
                return this.user;
            }

            set
            { 
                this.user = value;
                RaisePropertyChanged("User");
                RaisePropertyChanged("Anonymous");
                RaisePropertyChanged("Authenticated");
                Parameters.UserID = this.user != null ? this.user.UserID : 0;
            }
        }

        /// <summary>
        /// Gets or sets the customer.
        /// </summary>
        /// <value>The customer.</value>
        public Customer Customer
        {
            get
            {
                return this.customer;
            }

            set
            {
                this.customer = value;
                RaisePropertyChanged("Customer");
            }
        }

        /// <summary>
        /// Gets the create report command.
        /// </summary>
        public ICommand CreateReportCommand
        {
            get
            {
                return new RelayCommand(this.CreateReport);
            }
        }

        /// <summary>
        /// Gets the signin command.
        /// </summary>
        public ICommand SigninCommand
        {
            get
            {
                return new RelayCommand(Signin);
            }
        }

        /// <summary>
        /// Handles the create report command.
        /// </summary>
        private static void Signin()
        {
            HtmlPage.Window.Navigate(new Uri("/signin", UriKind.Relative));
        }

        /// <summary>
        /// Handles the create report command.
        /// </summary>
        private void CreateReport()
        {
            if (User == null)
            {
                HtmlPage.Window.Navigate(new Uri("/signin", UriKind.Relative));
            }
            else
            {
                Messenger.Default.Send(new NewReportMessage());
            }
        }

        /// <summary>
        /// Gets the user identity completed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Spenta.StreetCare.Silverlight.StreetCareAPI.GetUserIdentityCompletedEventArgs"/> instance containing the event data.</param>
        private void GetUserIdentityCompleted(object sender, GetUserIdentityCompletedEventArgs e)
        {
            try
            {
                if (e.Result.IsFault)
                {
                    LogHelper.Logger.WriteError("Error getting customer", e.Result.Fault.Exception);
                    return;
                }
            }
            catch (TargetInvocationException ex)
            {
                LogHelper.Logger.WriteError("Error getting customer", ex.InnerException ?? ex);
                return;
            }

            User = e.Result.User;

            Messenger.Default.Send(User);
        }
    }
}
